﻿using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxEditors;
using Kaysite.Controller;
using Kaysite.Manager;
using Kaysite.Model;
using System;
using System.Collections.Generic;

public partial class Controls_AdminControls_ArticleEdit : BaseEditControl<ArticleManager, ArticleController, Article>
{
    protected override ASPxCallback GetCallbackControl()
    {
        return clbkSave;
    }

    protected override ASPxButton GetCancelButton()
    {
        return btnCancel;
    }

    protected override ASPxButton GetOkButton()
    {
        return btnOk;
    }

    protected override string BackUrl
    {
        get
        {
            return string.Format("{0}?view=ArticleList", Request.Path);
        }
    }

    protected override void DoInitialize_1_BeforeLoadData()
    {
        base.DoInitialize_1_BeforeLoadData();
        txtDescription.SettingsImageUpload.UploadImageFolder =
            RepositoryManager.GetVirtualPath(RepositoryManager.Folders.News, true);
    }

    protected override void DoInitialize_2_LoadDataInitControls()
    {
        base.DoInitialize_2_LoadDataInitControls();
        InitComboEnum(ddlDisplayOption, e0Type.DontNeed,
            new List<eDisplayOption> { eDisplayOption.DisplayWithChild });
        InitComboEnum<eArticleStatus>(ddlStatus, e0Type.DontNeed);
    }

    protected override void InitControlsByModelData(Article model)
    {
        if (model == null)
        {
            ddlDisplayOption.Value = (int)eDisplayOption.Hide;
            txtOnDate.Value = DateTime.Now.Date;
            ddlStatus.Value = (int)eArticleStatus.Draft;
            return;
        }
        txtOnDate.Value = model.OnDate;
        txtName.Text = model.Name;
        txtShortDescription.Value = model.ShortDescription;
        txtDescription.Html = model.Description;
        ddlStatus.Value = (int)model.StatusID;
        ddlDisplayOption.Value = (int)model.DisplayInMain;
    }

    protected override void LoadModelDataFromControls(Article model)
    {
        model.OnDate = GetValue<DateTime>(txtOnDate);
        model.ArticleTypeID = eArticleType.News;
        model.Title = GetValue<string>(txtName);
        model.ShortDescription = GetValue<string>(txtShortDescription);
        model.Description = txtDescription.Html;
        model.StatusID = GetValue<eArticleStatus>(ddlStatus);
        model.DisplayInMain = GetValue<eDisplayOption>(ddlDisplayOption);
    }
}